package com.oracle.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.oracle.entity.po.EmployeePO;
import com.oracle.entity.po.SalePO;
import com.oracle.service.department.DepartmentService;
import com.oracle.service.employee.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/Employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DepartmentService departmentService;


    /**
     * [返回员工分页信息]
     *
     * @author lwf
     * @date 2019/12/27
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @version
     */
    @RequestMapping(value = "getEmployeePage", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getEmployeePage(@RequestParam("limit") int limit, @RequestParam("curr") int curr, HttpServletRequest request, HttpServletResponse response) {
        Page<EmployeePO> page = new Page<>(curr, limit);
        List list = employeeService.selectPage(page, null).getRecords();
        Integer count = employeeService.selectCount(null);
        Map<String, Object> jsonmap = new HashMap<String, Object>();
        jsonmap.put("code", 0);
        jsonmap.put("data", list);
        jsonmap.put("count", count);
        jsonmap.put("msg", "查询成功");
        return jsonmap;
    }

    /**
     * [返回员工业绩信息]
     *
     * @author lwf
     * @date 2019/12/28
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @version
     */
    @RequestMapping(value = "getPerformance", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getPerformancePage( HttpServletRequest request, HttpServletResponse response) {
        List list = employeeService.getPerformance();
        Map<String, Object> jsonmap = new HashMap<String, Object>();
        jsonmap.put("code", 0);
        jsonmap.put("data", list);
        jsonmap.put("msg", "查询成功");
        return jsonmap;
    }

    /**
     * [返回员工业绩信息详情]
     *
     * @param id 员工编号
     * @param request
     * @param response
     * @author lwf
     * @date 2019/12/28
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @version
     */
    @RequestMapping(value = "getPerformanceDetail", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getPerformanceDetail(@RequestParam("id") int id, HttpServletRequest request, HttpServletResponse response) {
        List list = (List) employeeService.getPerformanceDetail(id);
        Map<String, Object> jsonmap = new HashMap<String, Object>();
        jsonmap.put("code", 0);
        jsonmap.put("data", list);
        jsonmap.put("msg", "查询成功");
        return jsonmap;
    }



    @RequestMapping(value = "/editemployee", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> editemployee(@RequestBody String data, HttpServletRequest request, HttpServletResponse response) {
        JSONObject jsonObject = JSON.parseObject(data);
        EmployeePO employeePO = new EmployeePO();
        employeePO.setId(jsonObject.getInteger("id"));
        employeePO.setName(jsonObject.getString("name"));
        employeePO.setAge(jsonObject.getInteger("age"));
        employeePO.setDepartmentId(jsonObject.getInteger("departmentId"));
        employeePO.setGender(jsonObject.getString("gender"));
        employeePO.setJob(jsonObject.getString("job"));
        employeePO.setSalary(jsonObject.getInteger("salary"));
        employeeService.updateById(employeePO);
        Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("msg", "修改成功");
        return jsonMap;
    }

    @RequestMapping(value = "/deleteemployee", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteemployee(@RequestBody String data, HttpServletRequest request, HttpServletResponse response) {
        JSONArray jsonArray = JSON.parseArray(data);
        JSONObject jsonObject=jsonArray.getJSONObject(0);
        int id = jsonObject.getInteger("id");
        employeeService.deleteById(id);
        Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("code",0);
        jsonMap.put("msg", "删除成功");
        return jsonMap;
    }

    @RequestMapping(value = "/addemployee", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addemployee(@RequestBody String data, HttpServletRequest request, HttpServletResponse response) {
        JSONObject jsonObject = JSON.parseObject(data);
        EmployeePO employeePO = new EmployeePO();
        employeePO.setId(jsonObject.getInteger("id"));
        employeePO.setName(jsonObject.getString("name"));
        employeePO.setAge(jsonObject.getInteger("age"));
        employeePO.setDepartmentId(jsonObject.getInteger("departmentId"));
        employeePO.setGender(jsonObject.getString("gender"));
        employeePO.setJob(jsonObject.getString("job"));
        employeePO.setSalary(jsonObject.getInteger("salary"));
        employeeService.insert(employeePO);
        Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("msg", "添加成功");
        return jsonMap;
    }
    @RequestMapping(value = "/insertSale", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> insertSale(@RequestBody String data, HttpServletRequest request, HttpServletResponse response) {
        JSONObject jsonObject = JSON.parseObject(data);
        SalePO salePO = new SalePO();
        salePO.setEmployeeId(jsonObject.getInteger("employeeId"));
        salePO.setGoodsId(jsonObject.getInteger("goodsId"));
        salePO.setSaleNum(jsonObject.getInteger("saleNum"));
        employeeService.insertSale(salePO);
        Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("msg", "添加成功");
        return jsonMap;
    }



    @RequestMapping(value = "getDepartment",method = RequestMethod.GET)
    @ResponseBody
    public  Map<String, Object> getDepartment(){
        List list = departmentService.selectList(null);
        Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("data", list);
        return jsonMap;
    }
}